﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class ManageProductVersion : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindVersionNumber();
            }
        }

        private void BindVersionNumber()
        {
            ProductClassService classService = new ProductClassService();
            productVersionGridView.DataSource = classService.GetVersions();
            productVersionGridView.DataBind();
        }

        protected void productVersionGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < productVersionGridView.PageCount)
            {
                productVersionGridView.PageIndex = e.NewPageIndex;
            }
            
            productVersionGridView.DataBind();
        }

        protected void editLinkButton_Click(object sender, EventArgs e)
        {
            string versionNumberID = ((LinkButton)sender).CommandArgument;
            ProductClassService classService = new ProductClassService();
            ProductVersion version = classService.GetVersionByID(Convert.ToInt32(versionNumberID));
            operateButton.CommandArgument = version.ID.ToString();
            versionTextBox.Text = version.Name;
            operateButton.Text = "更新";
        }

        protected void deleteLinkButton_Click(object sender, EventArgs e)
        {
            string versionNumberID = ((LinkButton)sender).CommandArgument;
            ProductClassService classService = new ProductClassService();
            classService.DeleteVersionByID(Convert.ToInt32(versionNumberID));
            BindVersionNumber();
        }

        protected void productVersionGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void operateButton_Click(object sender, EventArgs e)
        {
            ProductClassService classService = new ProductClassService();
            if (operateButton.Text.Equals("编辑"))
            {
                if (classService.IsExistedVersionEdit(versionTextBox.Text))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('这个版本号已经存在，请更换一个不存在的版本号');", true);
                }
                else
                {
                    ProductVersion version = classService.GetVersionByID(Convert.ToInt32(((Button)sender).CommandArgument));
                    version.Name = versionTextBox.Text;

                    classService.UpdateVersion(version);
                }
            }
            else
            {
                if (classService.IsExistedVersion(versionTextBox.Text))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('这个版本号已经存在，请更换一个不存在的版本号');", true);
                }
                else
                {
                    ProductVersion newVersion = new ProductVersion();
                    newVersion.Name = versionTextBox.Text;

                    classService.AddNewProductVersion(newVersion);
                }
            }
            versionTextBox.Text = "";
            operateButton.Text = "添加";
            BindVersionNumber();
        }
    }
}
